package org.example.com.utils;

import java.io.InputStream;
import java.net.URL;
import java.util.Date;

import com.aliyun.oss.ClientBuilderConfiguration;
import com.aliyun.oss.ClientException;
import com.aliyun.oss.HttpMethod;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.common.auth.CredentialsProvider;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.GeneratePresignedUrlRequest;

/** 阿里云OSS服务 工具类 */
public class AliOssUtil {
	// 全局变量
    private static final String END_POINT = "https://oss-cn-shanghai.aliyuncs.com";
    private static final String ACCESS_KEY_ID="LTAI5tEanogqs4fcGzWfPaKs";
    private static final String ACCESS_KEY_SECRET="WoEz1JS066kdV5AvGSGzPywV8WXMWX";
    private static final String BUCKET_NAME = "sky-icast-xijure";

    public static String uploadFile(String objectName, InputStream in) throws Exception {
    	String region = "cn-shanghai";
    	CredentialsProvider provider=new DefaultCredentialProvider(ACCESS_KEY_ID,ACCESS_KEY_SECRET);
        // 创建OSSClient实例。
        // 当OSSClient实例不再使用时，调用shutdown方法以释放资源。
        ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
        clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);        
        OSS ossClient = OSSClientBuilder.create()
            .endpoint(END_POINT)
            .credentialsProvider(provider)
            .clientConfiguration(clientBuilderConfiguration)
            .region(region)               
            .build();
        try {
        	// 上传文件流
        	ossClient.putObject(BUCKET_NAME, objectName, in);
        	
            // 设置签名URL过期时间为15天
            Date expiration = new Date(System.currentTimeMillis() + 3600*1000*24*15);

            // 创建预签名URL请求
            GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(BUCKET_NAME, objectName,HttpMethod.GET);
            request.setExpiration(expiration);

            // 生成预签名URL
            URL signedUrl = ossClient.generatePresignedUrl(request);

            // 返回预签名URL
            return signedUrl.toString();
            
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        return "";
    }
}